<?php
class Notes extends MY_Controller 
{
	function __construct()
	{
		parent::__construct();

		$this->load->model(array('/public/note_model'));

		$this->validation->set_fields($this->note_model->fields);
	}
	
	function create()
	{
		$note_user_ID = $this->auth->getID();

		if (count($_POST))
		{
			if (!isset($_POST['note_model_ID']) || empty($_POST['note_model_ID']))
			{
				$_POST['note_model_ID'] = $this->auth->getSessionKey();
			}
			
			$rules = array('note_model', 'note_text');
			$this->validation->set_rules($this->note_model->getRules($rules));
			
			if ($this->validation->run() === TRUE)
			{
				$item['note_model'] = $this->validation->note_model;
				$item['note_model_ID'] = $this->validation->note_model_ID;
				$item['note_text'] = $this->validation->note_text;
				$item['note_user_ID'] = $note_user_ID;
				$item['note_created'] = MYSQL_NOW();

				if ($inserted_ID = $this->note_model->insert($item))
				{
					echo sprintf(
						'<tr>' .
							'<td class="note_date">%s (%s)</td>' .
							'<td>%s</td>' .
							'<td>' .
								'<a class="chatWidgetDeleteMessage" href="/admin/notes/delete/%d" onclick="return false;">' .
									'<img src="/layout/admin/img/btn_delete_note.png" width="11" height="11" alt="Delete Comment" title="Delete Comment" />' .
								'</a>' .
							'</td>' .
						'</tr>',
						date('Y-m-d', strtotime($item['note_created'])), $this->auth->user['user_login'], $item['note_text'], $inserted_ID);
				}
			}
		}
	}
	
	function delete($id)
	{
		$this->note_model->delete($id);
	}
}
?>